package com.wn.class30.service.impl;

import com.wn.class30.entity.EchartCount;
import com.wn.class30.entity.MyorderExample;
import com.wn.class30.entity.UserExample;
import com.wn.class30.mapper.MyorderDao;
import com.wn.class30.mapper.UserDao;
import com.wn.class30.service.CountService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class CountServiceImpl implements CountService {

    @Resource
    private MyorderDao myorderDao;
    @Resource
    private UserDao userDao;
    @Override
    public List<Integer> selectOrderCount(List<EchartCount> list) throws Exception {
        List<Integer> orderCount = new ArrayList();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (EchartCount echartCount : list) {
            MyorderExample myorderExample = new MyorderExample();
            MyorderExample.Criteria criteria = myorderExample.createCriteria();
            Date time1 = simpleDateFormat.parse(echartCount.getName());
            Calendar c = Calendar.getInstance();
            c.setTime(time1);
            c.add(Calendar.DAY_OF_MONTH, 1);
            Date time2 = c.getTime();
            criteria.andMyorderTimeBetween(time1,time2);
            long l = myorderDao.countByExample(myorderExample);
            orderCount.add((int) l);
        }
        return orderCount;
    }

    @Override
    public List<Integer> selectUserCount(List<EchartCount> list) throws Exception {
        List<Integer> userCount = new ArrayList();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        EchartCount fis = list.get(0);
        System.out.println(fis);
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        Date time1 = simpleDateFormat.parse(fis.getName());
        criteria.andRegistertimeLessThanOrEqualTo(time1);
        int total = (int) userDao.countByExample(userExample);

        for (EchartCount echartCount : list) {
            UserExample userExample2 = new UserExample();
            UserExample.Criteria criteria2 = userExample2.createCriteria();
            Date time = simpleDateFormat.parse(echartCount.getName());
            Calendar c = Calendar.getInstance();
            c.setTime(time);
            c.add(Calendar.DAY_OF_MONTH, 1);
            Date time2 = c.getTime();
            criteria2.andRegistertimeBetween(time,time2);
            int l = (int) userDao.countByExample(userExample2);
            total+=l;
            userCount.add(total);
        }
        return userCount;
    }

    @Override
    public List<Integer> selectUserCountRe(List<EchartCount> list) throws Exception {

        return null;
    }
}
